# 导包
from api.home import HomeAPI
from api.order import OrrderAPI
from common.dbutil import DBUtil
import config


# 创建测试用例
class TestYeWu:
    id888 = None
    order666 = None

    # 前置
    def setup(self):
        self.home_api = HomeAPI()
        self.add_api = OrrderAPI()
        self.dbu_api = DBUtil()

    # 获取轮播图
    def test01_get_banner(self):
        response = self.home_api.get_banner_info()
        # print(response.json())
        # assert response.status_code == 200
        # 提取商品id保存到环境变量
        TestYeWu.id888 = response.json().get("items")[0].get("key_word")
        print(TestYeWu.id888)

    # 创建订单
    def test02_add(self):
        add_data = {"products": [{"product_id": TestYeWu.id888, "count": 1}]}
        response = self.add_api.add(add_data)
        print(response.json())
        # 提取订单号
        TestYeWu.order666=response.json().get("order_id")
        print(TestYeWu.order666)

    # 查看订单（检查支付状态）
    def test03_select(self):
        response = self.add_api.select(TestYeWu.order666)
        print(response.json())

        # 连接数据库修改支付状态（1未付款-》2已支付）
        self.dbu_api.exe_sql(f"update `order` set status = 2 where id ={TestYeWu.order666};")

    # 查看订单
    def test04_select(self):
        response = self.add_api.select(TestYeWu.order666)
        print(response.json())
